// pages/Settlement/OpenCard/OpenCard.js
const controllers = require('../../controllers/controllerIndex.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    click:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that =this
    let vipList=[]

    


    controllers.vipShopList({
      type: 1,//0默认1商城页面分类列表2店铺入驻分类列表3系统新增商城分类
      shopId:wx.getStorageSync('shopId'),
      sessionId:wx.getStorageSync('token'),
      state:1
    }).then(res =>{

      res.data.forEach(item =>{
        item.useTime=that.getDate(item.endTime)
        vipList.push(item);
        
      })
      this.setData({
        vipList: res.data,
        time:vipList,
      })
     
    })
  },
  bindKeyInput(res) {
    console.log(res)
    this.setData({
      input: res.detail.value
    })


  },
  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false,
      click:true
    });
  },


  inputPhone(res){
    this.setData({

        phone:  res.detail.dataset
    })
  
 
  },
  inputName(res){
    this.setData({
      name:  res.detail.dataset
  })
  },
  optionTap(res) {
    var that = this;
    controllers.selectByMobile({
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId'),
      mobile: this.data.input
    }).then(res => {
      that.setData({
        userList: res.data,
        click:false
      })

    })
  },
  selectUser(res){
  
  
    this.setData({
     
      selectItem: res.currentTarget.dataset.item,
      show:false
    })
   },


   
  onAddAccount() {
    let mobile = this.data.input||"";
    let url = '/pages/userStatistics/UserEdit/UserEdit?callback=onAccountResult&mobile='+mobile;
    wx.navigateTo({url:url})
  },
  onAccountResult(res) {
    let that = this;
    let params={
      sessionId: wx.getStorageSync('token'),
      shopId:wx.getStorageSync('shopId'),
      accountRelationId:res.id
    }
    controllers.accountDetail(params).then((res)=>{//转换对象
      let data = res.data;
      let item = {
        id:data.id,
        accountId:data.accountId,
        mobile:data.accountMobile,
        nick:data.accountNick
      };
      that.setData({
        selectId: item.id,
        selectItem: item,
        show:false
      });
    });

  },



  selectVip(res){
    console.log(res)
    this.setData({
     selectId:res.currentTarget.dataset.id
    })
  },
  selectType(res){
    console.log(res)
    this.setData({
      payType: res.currentTarget.dataset.type
    })
  },
  getDate(time){
   
    let newTime=new Date().getTime();
    let  lastDay =0
    if( time){
     
       let endTime=new Date(time.replace(/-/g,"/")).getTime();
      lastDay=Math.ceil((endTime-newTime)/1000/24/3600);
      
     
      return lastDay
      }else
      return lastDay
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  cancel() {
    wx.navigateBack({})
  },
  openCard() {
    let that =this
    if(that.data.selectItem==null){
      wx.showToast({
        title: '请选择用户',
        type: 'none'
      })
      return
    }
     
    if(that.data.selectId==null){
      wx.showToast({
        title: '请选择会员卡类型',
        type: 'none'
      })
      return
    }
    let json ={
      vipId:that.data.selectId,
     
      shopId:wx.getStorageSync('shopId'),
      sessionId:wx.getStorageSync('token'),
      accountId:that.data.selectItem.accountId,
    }
    if( that.data.payType){
      json.payType=that.data.payType
    }
    controllers.openVip(json).then(res =>{
  
      wx.navigateBack({
    
        success: ()=>{
          wx.showToast({
            title: '开卡成功'
          });
        }
      })
    }
    )

   
    // wx.navigateTo({
    //   url: '../vipOpenCard/VipOpenCard',
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})